package com.ybb.vue.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ybb.vue.common.R;
import com.ybb.vue.common.Result;
import com.ybb.vue.common.util.ExcelUtils;
import com.ybb.vue.common.util.OperationRecordsUtils;
import com.ybb.vue.domain.GoodsInStatusEnum;
import com.ybb.vue.domain.GoodsInTypeEnum;
import com.ybb.vue.domain.GoodsTypeEnum;
import com.ybb.vue.domain.dto.GoodsInAddDto;
import com.ybb.vue.domain.dto.GoodsInByNameAndTypeDto;
import com.ybb.vue.domain.vo.EpmResidentExportVo;
import com.ybb.vue.domain.vo.GoodsInExportVo;
import com.ybb.vue.entity.Approval;
import com.ybb.vue.entity.GoodsIn;
import com.ybb.vue.mapper.GoodsInMapper;
import com.ybb.vue.mapper.GoodsInventoryMapper;
import com.ybb.vue.service.ApprovalService;
import com.ybb.vue.service.GoodsInService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ybb
 * @version 1.0
 * @Description 物资入库controller
 * @Date 2023/4/4 19:36
 */

@RestController
@RequestMapping("/goodsIn")
public class GoodsInController {

    @Autowired
    private GoodsInService goodsInService;

    @Autowired
    private GoodsInMapper goodsInMapper;

    @Autowired
    private ApprovalService approvalService;

    /**
     * 物资入库列表
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/goodsInList")
    @PreAuthorize("hasAuthority('epidemic:goods:inlist')"+"||"+"hasAuthority('epidemic:goods:inquery')")
    public Result<?> goodsInList(@RequestParam(defaultValue = "1") Integer pageNum,
                                 @RequestParam(defaultValue = "10") Integer pageSize,
                                 @RequestParam(defaultValue = "") String search) {
        LambdaQueryWrapper<GoodsIn> queryWrapper = Wrappers.<GoodsIn>lambdaQuery();
        queryWrapper.like(GoodsIn::getGoodsName, search);
        queryWrapper.orderByDesc(GoodsIn::getInTime);
        Page<GoodsIn> result = goodsInService.page(new Page<>(pageNum, pageSize), queryWrapper);
        List<GoodsIn> goodsInList = result.getRecords();
        goodsInList.forEach(goodsIn -> {
            // 入库状态转译
            goodsIn.setInStatus(GoodsInStatusEnum.getGoodsInStatus(goodsIn.getInStatus()));
            // 物资类别转译
            goodsIn.setGoodsType(GoodsTypeEnum.getGoodsType(goodsIn.getGoodsType()));
            // 入库类型转译
            goodsIn.setInType(GoodsInTypeEnum.getGoodsInType(goodsIn.getInType()));
        });
        return Result.success(result);
    }

    /**
     * 导出
     *
     * @param response
     * @param search
     */
    @GetMapping("/export")
    @PreAuthorize("hasAuthority('epidemic:goods:inexport')")
    public void exportResident(HttpServletResponse response, @RequestParam(defaultValue = "") String search) {
        List<GoodsIn> goodsInList = goodsInMapper.selectList(new QueryWrapper<GoodsIn>().lambda().like(GoodsIn::getGoodsName, search));
        List<GoodsInExportVo> exportList = new ArrayList<>();
        goodsInList.forEach(goodsIn -> {
            GoodsInExportVo goodsInExportVo = new GoodsInExportVo();
            BeanUtils.copyProperties(goodsIn, goodsInExportVo);
            // 入库状态转译
            goodsInExportVo.setInStatus(GoodsInStatusEnum.getGoodsInStatus(goodsInExportVo.getInStatus()));
            // 物资类别转译
            goodsInExportVo.setGoodsType(GoodsTypeEnum.getGoodsType(goodsInExportVo.getGoodsType()));
            // 入库类型转译
            goodsInExportVo.setInType(GoodsInTypeEnum.getGoodsInType(goodsInExportVo.getInType()));
            exportList.add(goodsInExportVo);
        });
        ExcelUtils.export(response, "入库单信息表", exportList, EpmResidentExportVo.class);

    }

    /**
     * 新增资产入库
     *
     * @param goodsInAddDto
     * @return
     */
    @PostMapping("/add")
    @PreAuthorize("hasAuthority('epidemic:goods:in')")
    @Transactional
    public Result<?> goodInAdd(@RequestBody @Valid GoodsInAddDto goodsInAddDto) {
        return goodsInService.add(goodsInAddDto);
    }

    /**
     * 物资清单要用，提供记录
     *
     * @param dto
     * @return
     */
    @PostMapping("/goodsInNotesByName")
    @PreAuthorize("hasAuthority('epidemic:goods:detail')")
    public Result<?> goodsInNotesByName(@RequestBody @Valid GoodsInByNameAndTypeDto dto) {
        LambdaQueryWrapper<GoodsIn> queryWrapper = Wrappers.<GoodsIn>lambdaQuery();
        queryWrapper.eq(GoodsIn::getGoodsName, dto.getGoodsName());
        queryWrapper.eq(GoodsIn::getGoodsType, dto.getGoodsType());
        queryWrapper.eq(GoodsIn::getInStatus, "1");
        queryWrapper.orderByDesc(GoodsIn::getInTime);
        List<GoodsIn> goodsInList = goodsInMapper.selectList(queryWrapper);
        goodsInList.forEach(goodsIn -> {
            // 入库状态转译
            goodsIn.setInStatus(GoodsInStatusEnum.getGoodsInStatus(goodsIn.getInStatus()));
            // 物资类别转译
            goodsIn.setGoodsType(GoodsTypeEnum.getGoodsType(goodsIn.getGoodsType()));
            // 入库类型转译
            goodsIn.setInType(GoodsInTypeEnum.getGoodsInType(goodsIn.getInType()));
        });
        return Result.success(goodsInList);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @DeleteMapping()
    @Transactional
    @PreAuthorize("hasAuthority('epidemic:goods:indelete')")
    public R delete(Long id) {
        GoodsIn goodsIn = goodsInMapper.selectById(id);
        approvalService.remove(new QueryWrapper<Approval>().lambda().eq(Approval::getSubjectId, id).eq(Approval::getSubjectSource, goodsIn.equals("0") ? "INA" : "INC"));
        goodsInService.removeById(id);
        return R.ok();
    }

    /**
     * 物资入库列表 不分页，详情使用
     *
     * @return
     */
    @GetMapping("/goodsInDetail")
    @PreAuthorize("hasAuthority('epidemic:goods:indetail')")
    public Result<?> goodsInDetail(Long id) {
        GoodsIn goodsIn = goodsInMapper.selectById(id);

        // 入库状态转译
        goodsIn.setInStatus(GoodsInStatusEnum.getGoodsInStatus(goodsIn.getInStatus()));
        // 物资类别转译
        goodsIn.setGoodsType(GoodsTypeEnum.getGoodsType(goodsIn.getGoodsType()));
        // 入库类型转译
        goodsIn.setInType(GoodsInTypeEnum.getGoodsInType(goodsIn.getInType()));

        return Result.success(goodsIn);
    }
}
